package com.drink.yan.svc.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drink.yan.core.mapper.*;
import com.drink.yan.intf.define.YanScoreTypeE;
import com.drink.yan.intf.entity.*;
import com.drink.yan.intf.entity.io.ResearchAreaIo;
import com.drink.yan.intf.vo.*;
import com.drink.yan.web.rest.req.qry.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 学校服务
 *
 * @Title:
 * @Package: com.drink.yan.svc.service.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023/11/24 20:44
 * @Version: v2.0
 */
@Service
public class YanSchoolService extends ServiceImpl<YanSchoolMapper, YanSchool> {

	@Value(value = "${school.province.area}")
	public String schoolProvinceArea;

	@Resource
	private YanSchoolRecruitMapper yanSchoolRecruitMapper;

	@Resource
	private YanMasterSpecialMapper yanMasterSpecialMapper;

	@Resource
	private YanSchoolAdjustMapper yanSchoolAdjustMapper;

	@Resource
	private YanSchoolRetestMapper yanSchoolRetestMapper;

	@Resource
	private YanSchoolRecruitUserMapper yanSchoolRecruitUserMapper;

	@Resource
	private SpecialService specialService;

	public List<YanSchool> listSchool(PageSchoolQry qry) {

		LambdaQueryWrapper<YanSchool> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(Objects.nonNull(qry.getSchoolName()), YanSchool::getSchoolName, qry.getSchoolName());
		queryWrapper.eq(Objects.nonNull(qry.getSchoolId()), YanSchool::getSchoolId, qry.getSchoolId());
		queryWrapper.in(Objects.nonNull(qry.getProvinces()), YanSchool::getProvince, qry.getProvinces());
		queryWrapper.eq(Objects.nonNull(qry.getProvince()), YanSchool::getProvince, qry.getProvince());

		LambdaQueryWrapper<YanSchool> customerWrapper = new LambdaQueryWrapper<>();
		if(!CollectionUtils.isEmpty(qry.getFeatures())){
			queryWrapper.and(wrapper -> {
				for(String feature : qry.getFeatures()){
					wrapper.or().like(YanSchool::getFeature, feature);
				}
			});
		}

		//"排名方式：US世界[us_sj],软科综合[rk_zh],校友会综合[xyh_zh],武书连[wsl],QS世界[qs_sj]"
		if("us_sj".equals(qry.getSortType())){
			queryWrapper.orderByAsc(YanSchool::getUsSj);
		}else if("rk_zh".equals(qry.getSortType())){
			queryWrapper.orderByAsc(YanSchool::getRkZh);
		}else if("xyh_zh".equals(qry.getSortType())){
			queryWrapper.orderByAsc(YanSchool::getXyhZh);
		}else if("wsl".equals(qry.getSortType())){
			queryWrapper.orderByAsc(YanSchool::getWsl);
		}else if("qs_sj".equals(qry.getSortType())){
			queryWrapper.orderByAsc(YanSchool::getQsSj);
		}


		//queryWrapper.select(YanSchool::getSchoolId, YanSchool::getSchoolName, YanSchool::getFeature, YanSchool::getProvince, YanSchool::get);
		return this.baseMapper.selectList(queryWrapper);
	}

	public Map<String, List<String>> schoolProvinceArea() {

		try {
			return JSONObject.parseObject(new String(schoolProvinceArea.getBytes("ISO-8859-1"), "utf-8"), Map.class);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<SchoolRecruitVo> listSchoolRecruit(ListSchoolSpeQry listSchoolSpeQry) {

		/*LambdaQueryWrapper<YanSchoolRecruit> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getSchoolId()), YanSchoolRecruit::getSchoolId, listSchoolSpeQry.getSchoolId());
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getYear()), YanSchoolRecruit::getYear, listSchoolSpeQry.getYear());
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getRecruitTypeName()), YanSchoolRecruit::getRecruitTypeName, listSchoolSpeQry.getRecruitTypeName());
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getSpecialCode()), YanSchoolRecruit::getSpecialCode, listSchoolSpeQry.getSpecialCode());
		queryWrapper.in(Objects.nonNull(listSchoolSpeQry.getLevel2CodeList()), YanSchoolRecruit::getLevel2Code, listSchoolSpeQry.getLevel2CodeList());*/

		List<SchoolRecruitVo> schoolRecruitVos = this.baseMapper.listSchoolRecruit(listSchoolSpeQry);
		schoolRecruitVos.forEach(x->{
			x.setFeatureList(JSONObject.parseArray(x.getFeature(), String.class));
			x.setResearchAreaList(JSONObject.parseArray(x.getResearchAreaListStr(), ResearchAreaIo.class));
		});
		return schoolRecruitVos;
	}

	public List<SchoolRecruitVo> listSchoolSpe(ListSchoolSpeQry listSchoolSpeQry) {

		/*LambdaQueryWrapper<YanSchoolRecruit> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getSchoolId()), YanSchoolRecruit::getSchoolId, listSchoolSpeQry.getSchoolId());
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getYear()), YanSchoolRecruit::getYear, listSchoolSpeQry.getYear());
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getRecruitTypeName()), YanSchoolRecruit::getRecruitTypeName, listSchoolSpeQry.getRecruitTypeName());
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getSpecialCode()), YanSchoolRecruit::getSpecialCode, listSchoolSpeQry.getSpecialCode());
		queryWrapper.in(Objects.nonNull(listSchoolSpeQry.getLevel2CodeList()), YanSchoolRecruit::getLevel2Code, listSchoolSpeQry.getLevel2CodeList());*/

		List<SchoolRecruitVo> schoolRecruitVos = this.baseMapper.listSchoolSpe(listSchoolSpeQry);
		schoolRecruitVos.forEach(x->{
			x.setFeatureList(JSONObject.parseArray(x.getFeature(), String.class));
		});
		return schoolRecruitVos;
	}

	public List<SchoolAdjustVo> listSchoolAdjust(ListSchoolSpeQry listSchoolSpeQry) {
		LambdaQueryWrapper<YanSchoolAdjust> queryWrapper = new LambdaQueryWrapper<>();
		return this.baseMapper.listSchoolAdjust(listSchoolSpeQry);
	}

	public List<YanSchoolRetest> listSchoolRetest(ListSchoolSpeQry listSchoolSpeQry) {
		LambdaQueryWrapper<YanSchoolRetest> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getSchoolId()), YanSchoolRetest::getSchoolId, listSchoolSpeQry.getSchoolId());
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getYear()), YanSchoolRetest::getYear, listSchoolSpeQry.getYear());
		queryWrapper.eq(Objects.nonNull(listSchoolSpeQry.getSpecialCode()), YanSchoolRetest::getSpecialCode, listSchoolSpeQry.getSpecialCode());
		return this.yanSchoolRetestMapper.selectList(queryWrapper);
	}

	public Object specialWork(String specialCode) {

		return "数据还没爬，不着急";
	}

	public YanSchool getSchoolDetail(Long schoolId) {

		LambdaQueryWrapper<YanSchool> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(YanSchool::getSchoolId, schoolId);
		YanSchool yanSchool = this.baseMapper.selectOne(wrapper);
		return yanSchool;
	}

	public YanSchool saveSchool(YanSchool school) {

		if(Objects.isNull(school.getSchoolId())){
			this.save(school);
		}else{
			YanSchool schoolDetail = this.getSchoolDetail(school.getSchoolId());
			schoolDetail.setSchoolName(school.getSchoolName());
			this.updateById(schoolDetail);
			school = schoolDetail;
		}
		return school;
	}

	public YanSchoolRecruit getSchoolRecruitDetail(Long recruitId) {

		return this.yanSchoolRecruitMapper.selectById(recruitId);
	}

	public YanSchoolRecruit saveSchoolRecruit(YanSchoolRecruit yanSchoolRecruit) {

		if(Objects.isNull(yanSchoolRecruit.getId())){
			this.yanSchoolRecruitMapper.insert(yanSchoolRecruit);
		}else{
			YanSchoolRecruit schoolRecruit = this.getSchoolRecruitDetail(yanSchoolRecruit.getId());
			Optional.ofNullable(yanSchoolRecruit.getDepartName()).ifPresent(schoolRecruit::setDepartName);
			Optional.ofNullable(yanSchoolRecruit.getResearchAreaList()).ifPresent(schoolRecruit::setResearchAreaList);
			//Optional.ofNullable(yanSchoolRecruit.getExamSubject()).ifPresent(schoolRecruit::setExamSubject);
			Optional.ofNullable(yanSchoolRecruit.getRecruitNumber()).ifPresent(schoolRecruit::setRecruitNumber);
			//Optional.ofNullable(yanSchoolRecruit.getResearchArea()).ifPresent(schoolRecruit::setResearchArea);
			Optional.ofNullable(yanSchoolRecruit.getRecruitUserNumber()).ifPresent(schoolRecruit::setRecruitUserNumber);
			Optional.ofNullable(yanSchoolRecruit.getMaxScore()).ifPresent(schoolRecruit::setMaxScore);
			Optional.ofNullable(yanSchoolRecruit.getMinScore()).ifPresent(schoolRecruit::setMinScore);
			Optional.ofNullable(yanSchoolRecruit.getSuggestScore()).ifPresent(schoolRecruit::setSuggestScore);


			this.yanSchoolRecruitMapper.updateById(schoolRecruit);
			yanSchoolRecruit = schoolRecruit;
		}
		return yanSchoolRecruit;
	}

	public YanSchoolAdjust saveSchoolAdjust(YanSchoolAdjust cmd) {

		if(Objects.isNull(cmd.getAdjustId())){
			this.yanSchoolAdjustMapper.insert(cmd);
		}else{
			YanSchoolAdjust schoolAdjust = this.getSchoolAdjustDetail(cmd.getAdjustId());
			Optional.ofNullable(cmd.getDepartName()).ifPresent(schoolAdjust::setDepartName);
			Optional.ofNullable(cmd.getRecruitNumber()).ifPresent(schoolAdjust::setRecruitNumber);
			Optional.ofNullable(cmd.getRemark()).ifPresent(schoolAdjust::setRemark);

			this.yanSchoolAdjustMapper.updateById(schoolAdjust);
			cmd = schoolAdjust;
		}
		return cmd;
	}

	public YanSchoolAdjust getSchoolAdjustDetail(Long adjustId) {
		return this.yanSchoolAdjustMapper.selectById(adjustId);
	}

	public YanSchoolRetest saveSchoolRetest(YanSchoolRetest cmd) {

		if(Objects.isNull(cmd.getRetestId())){
			this.yanSchoolRetestMapper.insert(cmd);
		}else{
			YanSchoolRetest schoolRetest = this.getSchoolRetestDetail(cmd.getRetestId());
			Optional.ofNullable(cmd.getSchoolId()).ifPresent(schoolRetest::setSchoolId);
			Optional.ofNullable(cmd.getYear()).ifPresent(schoolRetest::setYear);
			Optional.ofNullable(cmd.getDegreeTypeName()).ifPresent(schoolRetest::setDegreeTypeName);
			Optional.ofNullable(cmd.getDegreeType()).ifPresent(schoolRetest::setDegreeType);
			Optional.ofNullable(cmd.getSpecialCode()).ifPresent(schoolRetest::setSpecialCode);
			Optional.ofNullable(cmd.getSpecialName()).ifPresent(schoolRetest::setSpecialName);
			Optional.ofNullable(cmd.getDiffEnglish()).ifPresent(schoolRetest::setDiffEnglish);
			Optional.ofNullable(cmd.getDiffPolitics()).ifPresent(schoolRetest::setDiffPolitics);
			Optional.ofNullable(cmd.getDiffSpecialOne()).ifPresent(schoolRetest::setDiffSpecialOne);
			Optional.ofNullable(cmd.getDiffSpecialTwo()).ifPresent(schoolRetest::setDiffSpecialTwo);
			Optional.ofNullable(cmd.getDiffTotal()).ifPresent(schoolRetest::setDiffTotal);
			Optional.ofNullable(cmd.getEnglish()).ifPresent(schoolRetest::setEnglish);
			Optional.ofNullable(cmd.getPolitics()).ifPresent(schoolRetest::setPolitics);
			Optional.ofNullable(cmd.getSpecialOne()).ifPresent(schoolRetest::setSpecialOne);
			Optional.ofNullable(cmd.getSpecialTwo()).ifPresent(schoolRetest::setSpecialTwo);
			Optional.ofNullable(cmd.getTotal()).ifPresent(schoolRetest::setTotal);

			Optional.ofNullable(cmd.getNote()).ifPresent(schoolRetest::setNote);


			this.yanSchoolRetestMapper.updateById(schoolRetest);
			cmd = schoolRetest;
		}
		return cmd;
	}

	public YanSchoolRetest getSchoolRetestDetail(Long retestId) {

		return this.yanSchoolRetestMapper.selectById(retestId);
	}

	public Map<String, String> listSchoolSpe(PageSchoolSpeQry qry) {

		QueryWrapper<YanSchoolRecruit> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("distinct level2_code, level2_name")
				.lambda()
				.eq(Objects.nonNull(qry.getSchoolId()), YanSchoolRecruit::getSchoolId, qry.getSchoolId())
				.eq(Objects.nonNull(qry.getSchoolName()), YanSchoolRecruit::getSchoolName, qry.getSchoolName())
				.eq(Objects.nonNull(qry.getYear()), YanSchoolRecruit::getYear, qry.getYear())
				.eq(Objects.nonNull(qry.getRecruitTypeName()), YanSchoolRecruit::getRecruitTypeName, qry.getRecruitTypeName())
				.eq(Objects.nonNull(qry.getDegreeTypeName()), YanSchoolRecruit::getDegreeTypeName, qry.getDegreeTypeName())
				.isNotNull(YanSchoolRecruit::getLevel2Code);

		List<YanSchoolRecruit> yanSchoolRecruits = this.yanSchoolRecruitMapper.selectList(queryWrapper);
		return yanSchoolRecruits.stream().collect(Collectors.toMap(YanSchoolRecruit::getLevel2Code, YanSchoolRecruit::getLevel2Name, (v1 ,v2) -> v1));
	}

	public List<YanSchoolRecruitUser> listSchoolRecruitUser(ListRecruitUserQry qry) {

		LambdaQueryWrapper<YanSchoolRecruitUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Objects.nonNull(qry.getSchoolId()), YanSchoolRecruitUser::getSchoolId, qry.getSchoolId());
		queryWrapper.eq(Objects.nonNull(qry.getYear()), YanSchoolRecruitUser::getYear, qry.getYear());
		queryWrapper.eq(Objects.nonNull(qry.getSpecialCode()), YanSchoolRecruitUser::getSpecialCode, qry.getSpecialCode());
		queryWrapper.eq(Objects.nonNull(qry.getScoreType()), YanSchoolRecruitUser::getScoreType, qry.getScoreType());
		queryWrapper.eq(Objects.nonNull(qry.getBusinessKey()), YanSchoolRecruitUser::getBusinessKey, qry.getBusinessKey());

		return yanSchoolRecruitUserMapper.selectList(queryWrapper);
	}

	public YanSchoolRecruitUser saveSchoolRecruitUser(YanSchoolRecruitUser cmd) {

		if(YanScoreTypeE.recruit.getCode().equals(cmd.getScoreType())){
			YanSchoolRecruit schoolRecruit = yanSchoolRecruitMapper.selectById(cmd.getBusinessKey());
			cmd.setLevel1Code(schoolRecruit.getLevel1Code());
			cmd.setLevel1Name(schoolRecruit.getLevel1Name());
			cmd.setLevel2Code(schoolRecruit.getLevel2Code());
			cmd.setLevel2Name(schoolRecruit.getLevel2Name());
			cmd.setSpecialCode(schoolRecruit.getSpecialCode());
			cmd.setSpecialName(schoolRecruit.getSpecialName());
			cmd.setSchoolId(schoolRecruit.getSchoolId());
			cmd.setYear(schoolRecruit.getYear());
		}

		if(YanScoreTypeE.adjust.getCode().equals(cmd.getScoreType())){
			YanSchoolAdjust schoolRecruit = yanSchoolAdjustMapper.selectById(cmd.getBusinessKey());
			cmd.setLevel1Code(schoolRecruit.getLevel1Code());
			cmd.setLevel1Name(schoolRecruit.getLevel1Name());
			cmd.setLevel2Code(schoolRecruit.getLevel2Code());
			cmd.setLevel2Name(schoolRecruit.getLevel2Name());
			cmd.setSpecialCode(schoolRecruit.getSpecialCode());
			cmd.setSpecialName(schoolRecruit.getSpecialName());
			cmd.setSchoolId(schoolRecruit.getSchoolId());
			cmd.setYear(schoolRecruit.getYear());
		}

		if(Objects.isNull(cmd.getId())){
			this.yanSchoolRecruitUserMapper.insert(cmd);
		}else{
			YanSchoolRecruitUser schoolRecruitUser = this.schoolRecruitUserDetail(cmd.getId());
			Optional.ofNullable(cmd.getUserName()).ifPresent(schoolRecruitUser::setUserName);
			Optional.ofNullable(cmd.getFirstScore()).ifPresent(schoolRecruitUser::setFirstScore);

			this.yanSchoolRecruitUserMapper.updateById(schoolRecruitUser);
			cmd = schoolRecruitUser;
		}
		return cmd;
	}

	public YanSchoolRecruitUser schoolRecruitUserDetail(Long recruitUserId) {

		return yanSchoolRecruitUserMapper.selectById(recruitUserId);
	}

	public Map<Long, String> listSchoolMap() {

		LambdaQueryWrapper<YanSchool> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.select(YanSchool::getSchoolId, YanSchool::getSchoolName);
		List<YanSchool> yanSchools = this.baseMapper.selectList(queryWrapper);
		return yanSchools.stream().collect(Collectors.toMap(YanSchool::getSchoolId, YanSchool::getSchoolName));
	}

	public List<YanSpecialGjx> listSpecialGjx(SpecialGjxQry qry) {

		YanMasterSpecialVo yanMasterSpecialVo = specialService.specialDetail(qry.getSpecialCode());

		YanSchool schoolDetail = getSchoolDetail(qry.getSchoolId());

		List<YanSpecialGjx> yanSpecialGjxes = specialService.listSpecialGjx(yanMasterSpecialVo.getSpecialCode(), schoolDetail.getArea(), qry.getYear());
		if(CollectionUtils.isEmpty(yanSpecialGjxes)){
			yanSpecialGjxes = specialService.listSpecialGjx(yanMasterSpecialVo.getLevel2Code(), schoolDetail.getArea(), qry.getYear());
		}
		if(CollectionUtils.isEmpty(yanSpecialGjxes)){
			yanSpecialGjxes = specialService.listSpecialGjx(yanMasterSpecialVo.getLevel1Code(), schoolDetail.getArea(), qry.getYear());
		}
		return yanSpecialGjxes;
	}

	public List<RecruitUserSummaryVo> listRecruitUserSummary(SchoolRecruitQry qry) {

		if(Objects.nonNull(qry.getBusinessKey())){
			YanSchoolRecruit schoolRecruit = this.yanSchoolRecruitMapper.selectById(qry.getBusinessKey());
			qry.setSchoolId(schoolRecruit.getSchoolId());
			qry.setSpecialCode(schoolRecruit.getSpecialCode());
			qry.setDepartName(schoolRecruit.getDepartName());
			qry.setRecruitTypeName(schoolRecruit.getRecruitTypeName());

		}

		List<RecruitUserSummaryVo> list = this.baseMapper.listRecruitUserSummary(qry);
		/*list.forEach(x->{
			Integer selectLimit = null;
			if(Objects.nonNull(x.getRecruitUserNum())){
				selectLimit = x.getRecruitUserNum()/10;
			}

			SpeRecruitSuggestScoreQry scoreQry = qry.transform(SpeRecruitSuggestScoreQry.class);
			BigDecimal score = this.baseMapper.getRecruitUserSuggestScore(scoreQry);
			x.setSuggestScore(score);
		});*/
		return list;
	}

	public List<SchoolUserAdjustVo> listUserAdjust(PageUserAdjustQry qry) {

		List<SchoolUserAdjustVo> schoolUserAdjustVos = this.baseMapper.listUserAdjust(qry);
		schoolUserAdjustVos.forEach(x->{
			x.setScoreLtNum(x.getScoreLtNum() + 1);
			x.setSchoolNames(Arrays.asList(x.getSchoolNamesStr().split(",")));
		});
		return schoolUserAdjustVos;
	}
}
